package com.sdust.listener;

import com.sdust.constant.ParameterConstant;
import com.sdust.mapper.UserMapper;
import com.sdust.pojos.dto.UserInfoDto;
import com.sdust.pojos.entity.Comment;
import com.sdust.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringRabbitMQListener {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "testDirect.queue3"),
//            exchange = @Exchange(name = "testDirectExchange2",type = ExchangeTypes.DIRECT),
//            key = {"testRouting1","testRouting2"}
//    ))//默认类型为direct
//    public void queue01Listener(String msg) {
//        System.out.println("消费者1收到消息: "+msg);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "testDirect.queue4"),
//            exchange = @Exchange(name = "testDirectExchange2",type = ExchangeTypes.DIRECT),
//            key = {"testRouting3","testRouting4"}
//    ))//默认类型为direct
//    public void queue02Listener(String msg) {
//        System.out.println("消费者2收到消息: "+msg);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "testDirect.queueHeng"),
//            exchange = @Exchange(name = "testDirectExchangeHeng",type = ExchangeTypes.DIRECT),
//            key = {"testRoutingHeng1","testRoutingHeng2"}
//    ))//默认类型为direct
//    public void queueTestListener(String msg) {
//        System.out.println("消费者Heng收到消息: "+msg);
//    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "userInfoMQ"),
            exchange = @Exchange(name = "UserInfoDirectExchange",type = ExchangeTypes.DIRECT),
            key = {"userInfoMQ"}
    ))//默认类型为direct
    public void messageTestListener(UserInfoDto dto) {

        log.info("消费者-用户信息修改-消息转换器收到消息: {}", dto);

        //修改文章列表用户信息
        userMapper.updateArticleUserInfo(dto);

        //修改好友列表用户信息
        userMapper.updateFriendUserInfo(dto);

        //修改聊天记录用户信息
        userMapper.updateChatUserInfo(dto);
        userMapper.updateChatTargetUserInfo(dto);

        //修改评论数据用户信息
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(dto.getUserId()));
        Update update = new Update();
        update.set("userName", dto.getName());
        update.set("userAvatar", dto.getAvatarUrl());
        mongoTemplate.updateMulti(query, update, Comment.class, ParameterConstant.AP_ARTICLE_COMMENT);
    }
}